package com.kuang.test;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/** @ClassName: test4 @Description: TODO @Author NeySun @CreateTime 2022-04-02 17:58 @Version 1.0 */
@Slf4j
public class test4 {


    @Test
    public void sss() {
        //测试12
        String ss = "/data/rop/person.json";
        int one = ss.lastIndexOf(".");
        String Suffix = ss.substring((one + 1));
        System.out.println(Suffix);//json
        int one1 = ss.lastIndexOf("/");
        String Suffix1 = ss.substring((one1 + 1));
        System.out.println(Suffix1);//person . json
        String prefix = ss.substring(0, ss.lastIndexOf("/"));
        System.out.println(prefix);//data/rop
    }

    @Test
    public void test1() {
        Hashtable hashtable = new Hashtable();
        hashtable.put("key",1);
        boolean contains = hashtable.containsKey(1);
        System.out.println(contains);
        System.out.println();
    }

    @Test
    public void s2() {

        // 创建两个LocalDateTime对象
        LocalDateTime start = LocalDateTime.of(2023, 3, 8, 10, 10, 10);
        LocalDateTime end = LocalDateTime.of(2023, 3, 8, 12, 10, 10);
        LocalDateTime modle = LocalDateTime.of(2023, 3, 8, 11, 10, 10);

        if (modle.isAfter(start) && modle.isBefore(end)) {
            System.out.println("true");
        }

    }

    @Test
    public void s3() {
        List<User> userList = new ArrayList<>();
        List<User> userList2 = new ArrayList<>();
        User user = new User();
        user.setUsername("小明");
        user.setCreateTime(LocalDateTime.now());
        User use2 = new User();
        use2.setUsername("小花");
        use2.setCreateTime(LocalDateTime.now().plusDays(1));
        userList.add(use2);
        userList.add(user);
        List<User> collect = userList.stream().sorted(Comparator.comparing(User::getCreateTime)
                .reversed()).collect(Collectors.toList());

        extracted(userList);
        for (User user1 : collect) {
            userList2.add(user1);
        }
        extracted(userList2);
        //Iterator<User> iterator = userList.iterator();
        //while (iterator.hasNext()){
        //    User next = iterator.next();
        //    if (next.getUsername().equals("小明")) {
        //        userList2.add(next);
        //        iterator.remove();
        //    }
        //}
        //User use3 = new User();
        //use3.setUsername("小gou");
        //userList.add(use3);
        System.out.println(userList + "1");

        System.out.println(userList2 + "2");
    }

    private void extracted(List<User> userList) {
        System.out.println(userList);
    }

    @Test
    public void test14() {
        String aa = "2021-12-12";
        LocalDateTime localDateTime = LocalDate.parse(aa, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay();
        LocalDateTime localDateTime2 = localDateTime.minusMinutes(10L);
        System.out.println(localDateTime2.isBefore(localDateTime));
        System.out.println(localDateTime2);
        System.out.println(localDateTime);
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime1 = now.minusHours(24L);
        System.out.println(localDateTime1);

    }

    @Test
    public void test18() {
        List<String> list = new ArrayList<>();
        String aa = "4152";
        String ab = "54564gf";
        list.add(aa);
        list.add(ab);
        String join = String.join(",", list);
        System.out.println(join);
        String string = list.toString();
        String replace = string.replace("[", "").replace("]", "");
        //System.out.println(replace);

    }

    @Test
    public void test177() {
        User user = new User();
        user.setId(0);
        user.setUsername("yonghu1");
        // user.setAge(0);
        user.setAddress("");
        user.setUser_count(0);
        user.setCreateTime(LocalDateTime.now());
        long l = Optional.of(user).map(User::getAge).orElse(88).longValue();
        User user1 = Optional.of(user).get();
        System.out.println(user1);
        System.out.println(l);
    }
    @Test
    public void test13() {
        // 假设有一个包含BigDecimal类型数据的集合
        BigDecimal bigDecimal = new BigDecimal("1.0");
        BigDecimal bigDecimal2 = new BigDecimal("121212121");
        BigDecimal bigDecimal3 = new BigDecimal("1.0");
        BigDecimal bigDecimal4 = new BigDecimal("1.98");
        BigDecimal bigDecimal5 = new BigDecimal("2.22");
        List<User> numbers = new ArrayList<>();
        User user5 = new User();
        user5.setId(0);
        user5.setUsername("用户5");
        user5.setAge(0);
        user5.setAddress("");
        user5.setUser_count(0);
        user5.setVersion(bigDecimal5);
        user5.setCreateTime(LocalDateTime.now());
        String s = JSON.toJSONString(user5);
        System.out.println(s);
        Map<String, String> map = new HashMap<>();
        map.put("UserName", "yonghu8");
        String s1 = JSON.toJSONString(map);
        System.out.println(s1);
        User user = JSON.parseObject(s1, User.class);
        String s2 = JSON.toJSONString(user);
        System.out.println(s2);

        User user4 = new User();
        user4.setId(0);
        user4.setUsername("用户4");
        user4.setAge(0);
        user4.setAddress("");
        user4.setUser_count(0);
        user4.setVersion(bigDecimal4);
        user4.setCreateTime(LocalDateTime.now().minusDays(1));
        User user1 = new User();
        user1.setId(0);
        user1.setUsername("yonghu1");
        user1.setAge(0);
        user1.setAddress("");
        user1.setUser_count(0);
        user1.setCreateTime(LocalDateTime.now().plusDays(18));
        user1.setVersion(bigDecimal);


        User user2 = new User();
        user2.setId(0);
        user2.setUsername("yonghu2");
        user2.setAge(0);
        user2.setAddress("");
        user2.setUser_count(0);
        user2.setCreateTime(LocalDateTime.now().plusDays(3));
        user2.setVersion(bigDecimal2);


        User user3 = new User();
        user3.setId(0);
        user3.setUsername("yong3");
        user3.setAge(0);
        user3.setAddress("");
        user3.setUser_count(0);
        user3.setCreateTime(LocalDateTime.now().plusDays(4));
        user3.setVersion(bigDecimal3);
        numbers.add(user1);
        numbers.add(user2);
        numbers.add(user3);
        numbers.add(user4);
        numbers.add(user5);
        String aa = "0.9";
        BigDecimal target = null;
        if (StringUtils.isBlank(aa)) {
            target = new BigDecimal("0");
        } else {
            target = new BigDecimal(aa);
        }
        User closestValue = getClosestValue(numbers, target);
        System.out.println("Closest value: " + closestValue.getVersion() + "用户：" + closestValue + "time" + closestValue.getCreateTime());
    }

    public static User getClosestValue(List<User> list, BigDecimal target) {
        BigDecimal closest = null;
        for (User value : list) {
            if (Objects.isNull(value.getVersion()) || value.getVersion().compareTo(BigDecimal.ZERO) < 0) {
                value.setVersion(BigDecimal.ZERO);
            }
            if (value.getVersion().compareTo(target) <= 0) {
                if (closest == null || target.subtract(value.getVersion()).compareTo(target.subtract(closest)) <= 0) {
                    closest = value.getVersion();
                }
            }
        }
        BigDecimal finalClosest = closest;
        if (Objects.isNull(finalClosest)) {
            List<User> collect = list.stream()
                    .sorted(Comparator.comparing(User::getCreateTime).reversed())
                    .collect(Collectors.toList());
            return collect.get(0);
        }
        List<User> collect = list.stream()
                .filter(x -> x.getVersion().compareTo(finalClosest) == 0)
                .sorted(Comparator.comparing(User::getCreateTime).reversed())
                .collect(Collectors.toList());
        return collect.get(0);
    }


    @Test
    public void s4() {
        BigDecimal bigDecimal = new BigDecimal("21.1");
        BigDecimal bigDecimal2 = new BigDecimal("20.2");
        List<User> numbers = new ArrayList<>();
        User user1 = new User();
        user1.setId(0);
        user1.setUsername("");
        user1.setAge(0);
        user1.setAddress("");
        user1.setUser_count(0);
        user1.setVersion(bigDecimal2);

        User user2 = new User();
        user2.setId(0);
        user2.setUsername("");
        user2.setAge(0);
        user2.setAddress("");
        user2.setUser_count(0);
        user2.setVersion(bigDecimal);


        User user3 = new User();
        user3.setId(0);
        user3.setUsername("");
        user3.setAge(0);
        user3.setAddress("");
        user3.setUser_count(0);
        user3.setVersion(bigDecimal);
        numbers.add(user1);
        numbers.add(user2);
        numbers.add(user3);
        BigDecimal target = new BigDecimal("21");
        User closestObj = getClosestNumber(numbers, target);
        System.out.println("Closest number to " + target + " is: " + closestObj + "----" + closestObj.getVersion());

    }

    public static User getClosestNumber(List<User> numbers, BigDecimal target) {
        User closestObj = numbers.get(0);
        BigDecimal closestNumber = numbers.get(0).getVersion();
        BigDecimal minDifference = closestNumber.subtract(target).abs();
        for (int i = 1; i < numbers.size(); i++) {
            BigDecimal difference = numbers.get(i).getVersion().subtract(target).abs();
            if (difference.compareTo(minDifference) < 0) {
                minDifference = difference;
                closestObj = numbers.get(i);
            }
        }
        return closestObj;
    }

    //创建前端展示列表tree
    @Test
    public void s5() {
      Object aa = null;
        String bb = (String) aa;
        if(Objects.isNull(bb)){
            System.out.println(bb);
        }
      
    }

    public class lableValue {
        private String label;
        private String value;
        private Integer stars;

        public lableValue(String label, String value, Integer stars) {
            this.label = label;
            this.value = value;
            this.stars = stars;
        }

        public String getLabel() {
            return label;
        }

        public void setLabel(String label) {
            this.label = label;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public Integer getStars() {
            return stars;
        }

        public void setStars(Integer stars) {
            this.stars = stars;
        }
    }

    //创建前端展示列表tree
    @Test
    public void s56() {
        // m4();
        String str1="i";
        String str= "i";
        System.out.println(str1==str);
    }
    /**
     * 循环坐标去除重复
     */

    private static void m4()
    {
        List<Integer> initList = Arrays.asList(70,70,-1,5,3,3,4,4,4,4,99);
        List<Integer> srcList = new ArrayList<>(initList);
        List<Integer> newList = new ArrayList<>(initList);

        System.out.println(srcList.indexOf(70));
        System.out.println(srcList.lastIndexOf(70));


        for (Integer element : srcList) {
            if(newList.indexOf(element) != newList.lastIndexOf(element)) {
                newList.remove(newList.lastIndexOf(element));
            }
        }
        newList.forEach((s) -> System.out.print(s+", "));
        System.out.println();
        System.out.println();
    }
}
